﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System.Collections.Generic;
using DotNet.JCore.Entity;
using DotNet.Collections;
using DotNet.Extensions;
using DotNet.Utility;
using DotNet.Data;
using System;
using DotNet.Data.Utilities;
using System.Linq;

namespace DotNet.JCore.Service
{
    /// <summary>
    /// 服务
    /// </summary>
    public class BusRegDayRptService
    {
        //private static readonly Cache<string, BusRegDayRpt> Cache = new Cache<string, BusRegDayRpt>();
        private JCoreRepository<BusRegDayRpt> repos = new JCoreRepository<BusRegDayRpt>();

        /// <summary>
        /// 获取启用的对象集合(已排序)
        /// </summary>
        /// <param name="PointID">名称关键字</param>
        /// <param name="calculateDate">有限</param>
        public List<BusRegDayRpt> GetList(DateTime? startDate, DateTime? endDate, string PointID)
        {
            var query = repos.SQL.OrderByDesc(p => p.calculateDate);
            SetQuery(query, startDate, endDate, PointID);
            return repos.Fetch(query);
        }

        /// <summary>
        /// 获取对象分页集合
        /// </summary>
        /// <param name="pageCondition">分页对象</param>
        /// <param name="checkPointName">名称关键字</param>
        /// <param name="pointId">检查点</param>
        public PageList<BusRegDayRpt> GetPageList(PaginationCondition pageCondition,
           DateTime? startDate, DateTime? endDate, string PointID)
        {
            pageCondition.SetDefaultOrder(nameof(BusRegDayRpt.calculateDate));
            var query = repos.PageQuery(pageCondition);
            if (startDate.HasValue)
            {
                var startDateDt = startDate.ToDateTime();
                query.Where(p => p.calculateDate >= startDateDt);
            }

            if (endDate.HasValue)
            {
                var endDateDt = endDate.ToDateTime().ToDateTime();
                query.Where(p => p.calculateDate <= endDateDt);
            }

            if (PointID.IsNotEmpty())
            {
                PointID = PointID.Trim();
                query.Where(p => p.PointID == PointID);
            }

            return repos.Page(query);
        }

        /// <summary>
        /// 设置查询条件
        /// </summary>
        /// <param name="query">查询对象</param>
        /// <param name="search">数据对象</param>
        public void SetQuery(SQLQuery<BusRegDayRpt> query, DateTime? startDate, DateTime? endDate, string PointID)
        {

            if (startDate.HasValue)
            {
                var startDateDt = startDate.ToDateTime();
                query.Where(p => p.calculateDate >= startDateDt);
            }

            if (endDate.HasValue)
            {
                var endDateDt = endDate.ToDateTime().ToDateTime();
                query.Where(p => p.calculateDate <= endDateDt);
            }

            if (PointID.IsNotEmpty())
            {
                PointID = PointID.Trim();
                query.Where(p => p.PointID == PointID);
            }
        }

        /// <summary>
        ///  执行插入报表字段的存储过程，
        ///  主要是把当日的数据重新计算一遍
        /// </summary>
        public List<BusRegDayRpt> execproc_busreg(string procname)
        {
            var db = repos.Database;

            var store = new StoreProc(procname);
            //var par = new OutPutParam();
            //var store = new StoreProc("Proc_BusAutoReg",new { startDate="",endDate="",count = par });

            var list = db.Query<BusRegDayRpt>(store).ToList();
            return list;

        }
    }
}
